package com.veo.controller;

import com.github.pagehelper.PageInfo;
import com.veo.domain.Permission;
import com.veo.domain.Role;
import com.veo.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/roleController")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("/findAllRole")
    public ModelAndView findAllRole(@RequestParam(value = "page",defaultValue = "1",required = false)Integer page,@RequestParam(value = "size",defaultValue = "5",required = false)Integer size,@RequestParam(value = "searcherName",defaultValue = "",required = false)String searcherName) throws UnsupportedEncodingException {
        ModelAndView mv=new ModelAndView();
        //判断是乱码 (GBK包含全部中文字符；UTF-8则包含全世界所有国家需要用到的字符。)
        if (!(Charset.forName("GBK").newEncoder().canEncode(searcherName))) {
            //转码UTF8
            searcherName = new String(searcherName.getBytes("ISO-8859-1"), "utf-8");
        }
        List<Role> allRole = roleService.findAllRole(page,size,searcherName);
        //保存分页信息
        PageInfo roleList=new PageInfo(allRole);
        mv.addObject("roleList",roleList);
        mv.addObject("searcherName",searcherName);
        mv.setViewName("role-list");
        return mv;
    }

    @RequestMapping("addRole")
    public String addRole(Role role){
        String id= UUID.randomUUID().toString().replace("-","").toUpperCase();
        role.setId(id);
        int coutn = roleService.addRole(role);
        return "redirect:findAllRole";
    }

    @RequestMapping("/findPermissionToRole")
    public ModelAndView findPermissionToRole(@RequestParam(value = "id",required = true) String roleId){
        ModelAndView mv=new ModelAndView();
        Role role = roleService.findById(roleId);
        List<Permission> permissionToRole = roleService.findPermissionToRole(roleId);
        mv.addObject("role",role);
        mv.addObject("permissionList",permissionToRole);
        mv.setViewName("role-permission-add");
        return mv;
    }

    @RequestMapping("/addPermissionToRole")
    public String addPermissionToRole(@RequestParam(name = "roleId",required = true)String roleId,@RequestParam(name = "ids",required = true)String[] permissionIds){
        int count = roleService.addPermissionToRole(roleId, permissionIds);
        return "redirect:findAllRole";
    }

    @RequestMapping("/findRoleById")
    public ModelAndView findRoleById(String id){
        ModelAndView mv=new ModelAndView();
        Role role = roleService.findRoleAndPermissions(id);
        mv.addObject("role",role);
        mv.setViewName("role-show");
        return mv;
    }

    @RequestMapping("/deleteRoleByIds")
    public String deleteRoleByIds(String idStr){
        if (idStr!=null && idStr.length()>0&& idStr!=""){
            String[] ids = idStr.split(",");
            for (String id : ids) {
                roleService.deleteRolePermissionId(id);
                roleService.deleteRoleById(id);
            }
        }
        return "redirect:findAllRole";
    }


}
